﻿var xPoint = 0;
var yPoint = 0;
var thumbLeft = 0;
var thumbTop = 0;
var scrollerclicked = false;
var scrollobject = null;
var scrollview = null;
var b = false;

function scroller_vertical(scroller) {
    if (scroller.hasClass("scrollery")) {
        return true;
    }
    return false;
}

function createScoller(id) {
    var scroller = $("#" + id);
    var scrollbar = scroller.children(".scrollbar");
    var thumb = scrollbar.children(".scrollbar_thumb");
    var view = scroller.children(".scroller_view");
    var panel = view.children(".scroller_panel");
    var ratio = view.width() / (panel.width() - 30);
    if (scroller_vertical(scroller)) {
        ratio = view.height() / (panel.height());
    }
    if (ratio <= 0 || ratio >= 1) {
        scrollbar.hide();
        if (scroller_vertical(scroller)) {
            view.css("right", 0);
        }
    }
    else {
        scrollbar.show();
        if (scroller_vertical(scroller)) {
            view.css("right", 20);
            var height = scrollbar.height() * ratio;
            height = Math.floor(height + 0.5);
            thumb.height(height);
        } else {
            var width = scrollbar.width() * ratio;
            width = Math.floor(width + 0.5);
            thumb.width(width);
        }
        scrollbar
        .mousedown(function (e) {
            if (scrollerclicked) {
                b = false;
            }
            else {
                b = true;
            }
        })
        .click(function (e) {
            if (b) {
                var thumb = $(this).children(".scrollbar_thumb");
                if (scroller_vertical(scroller)) {
                    var yNew = e.pageY - $(this).offset().top - (thumb.height() + 11) / 2;
                    if (yNew < 0) {
                        yNew = 0;
                    }
                    if (yNew > $(this).height() - thumb.height()) {
                        yNew = $(this).height() - thumb.height();
                    }
                    thumb.css("top", yNew);
                } else {
                    var xNew = e.pageX - $(this).offset().left - (thumb.width() + 11) / 2;
                    if (xNew < 0) {
                        xNew = 0;
                    }
                    if (xNew > $(this).width() - thumb.width()) {
                        xNew = $(this).width() - thumb.width();
                    }
                    thumb.css("left", xNew);
                }
                //alert(xNew);
                thumb.trigger("scrolling");
            }
        });
        thumb
        .bind('scrolling', function (e) {
            var thumb = $(this);
            var scrollbar = thumb.parent(".scrollbar");
            var p = scrollbar.parent(".scroller");
            var view = p.children(".scroller_view");
            var panel = view.children(".scroller_panel");
            if (scroller_vertical(p)) {
                var posNew = (panel.height() - view.height()) * thumb.position().top / (scrollbar.height() - thumb.height());
                panel.css("top", -posNew);
            }
            else {
                var posNew = (panel.width() - 30 - view.width()) * thumb.position().left / (scrollbar.width() - thumb.width());
                panel.css("left", -posNew);
            }
        })
        .mousedown(function (e) {
            $("body").disableTextSelect().css("cursor", "default");
            xPoint = e.pageX;
            yPoint = e.pageY;
            thumbLeft = $(this).position().left;
            thumbTop = $(this).position().top;
            scrollerclicked = true;
            scrollobject = $(this);
            disableSelecting();
        });
        $(document)
        .mousemove(function (e) {
            if (scrollerclicked && scrollobject) {
                var xNew = thumbLeft + e.pageX - xPoint;
                var yNew = thumbTop + e.pageY - yPoint;
                var scrollbar = scrollobject.parent(".scrollbar");
                var w1 = scrollbar.width();
                var w2 = scrollobject.width();
                var h1 = scrollbar.height();
                var h2 = scrollobject.height();
                if (yNew < 0) {
                    yNew = 0;
                }
                if (yNew > h1 - h2) {
                    yNew = h1 - h2;
                }
                if (xNew < 0) {
                    xNew = 0;
                }
                if (xNew > w1 - w2) {
                    xNew = w1 - w2;
                }
                if (scroller_vertical(scrollbar.parent())) {
                    scrollobject.css("top", yNew);
                } else {
                    scrollobject.css("left", xNew);
                }
                scrollobject.trigger("scrolling");
            }
        })
        .mouseup(function (e) {
            $("body").enableTextSelect().css("cursor", "default");
            if (scrollerclicked) {
                scrollerclicked = false;
                scrollobject = null;
                enableSelecting();
            }
        });
        var views = scroller.children(".scroller_view").toArray();
        for (var i = 0; i < views.length; i++) {
            //adding the event listerner for Mozilla
            if (window.addEventListener) {
                views[i].addEventListener('DOMMouseScroll', scroller_scroll, false);
                views[i].addEventListener('mousewheel', scroller_scroll, false);
            }
            //for IE/OPERA etc
            views[i].onmousewheel = scroller_scroll;
        }
        scroller.children(".scroller_view")
        .mouseenter(function (e) {
            scrollview = $(this);
        })
        .mouseleave(function (e) {
            scrollview = null;
        });
    }
}

function scroller_update(id) {
    var scroller = $("#" + id);
    var scrollbar = scroller.children(".scrollbar");
    var thumb = scrollbar.children(".scrollbar_thumb");
    var view = scroller.children(".scroller_view");
    var panel = view.children(".scroller_panel");
    var ratio = view.width() / (panel.width() - 30);
    if (scroller_vertical(scroller)) {
        ratio = view.height() / (panel.height() - 30);
    }
    thumb.css({ "left": 0, "top": 0 });
    thumb.trigger("scrolling");
    if (ratio >= 1) {
        scrollbar.hide();
        if (scroller_vertical(scroller)) {
            view.css("right", 0);
        }
    }
    else {
        scrollbar.show();
        if (scroller_vertical(scroller)) {
            view.css("right", 20);
            var height = scrollbar.height() * ratio;
            height = Math.floor(height + 0.5);
            thumb.height(height);
        } else {
            var width = scrollbar.width() * ratio;
            width = Math.floor(width + 0.5);
            thumb.width(width);
        }
    }
}


function scroller_scroll(event) {
    var delta = 0;
    if (!event) event = window.event;

    // normalize the delta
    if (event.wheelDelta) {
        // IE and Opera
        delta = event.wheelDelta / 60;
    } else if (event.detail) {
        // W3C
        delta = -event.detail / 2;
    }
    var p = scrollview.parent(".scroller");
    var scrollbar = p.children(".scrollbar");
    var thumb = scrollbar.children(".scrollbar_thumb");
    if (scroller_vertical(p)) {
        var yNew = thumb.position().top - delta * 5;
        var h1 = scrollbar.height();
        var h2 = thumb.height();
        if (yNew < 0) {
            yNew = 0;
        }
        if (yNew > h1 - h2) {
            yNew = h1 - h2;
        }
        if (thumb.position().top != yNew) {
            event.preventDefault();
        }
        thumb.css({ "top": yNew });
    }
    else {
        var xNew = thumb.position().left - delta * 5;
        var w1 = scrollbar.width();
        var w2 = thumb.width();
        if (xNew < 0) {
            xNew = 0;
        }
        if (xNew > w1 - w2) {
            xNew = w1 - w2;
        }
        if (thumb.position().left != xNew) {
            event.preventDefault();
        }
        thumb.css({ "left": xNew });
    }
    thumb.trigger("scrolling");
    return false;
}